package org.raymond.iworks.study.redis.reactive;

import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.publisher.Flux;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
public class Demo1 {
    // 发布者有两种: Mono, Flux
    // Mono发布0到1个, Flux发布0到N个
    public static void main(String[] args) {
        test2();
    }

    public static void test1(){
        Flux.just("diablo", "sky", "raymond").subscribe(new Subscriber<String>() {
            @Override
            public void onSubscribe(Subscription subscription) {
                subscription.request(3);
            }
            @Override
            public void onNext(String s) {
                log.info("hello {}", s);
            }
            @Override
            public void onError(Throwable throwable) {
                log.error("{}", throwable.getMessage());
            }
            @Override
            public void onComplete() {
                log.info("completed");
            }
        });
    }

    // test1的简化写法
    public static void test2(){
        Flux.just("diablo", "sky", "raymond")
                .doOnNext(s->log.info("hello {}", s))
                .doOnComplete(()->log.info("complete"))
                //.take(2) // 只处理前n个
                .subscribe();
    }
}
